package gq.example.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import gq.example.dto.FindUserDto;
import gq.example.dto.LoginDto;
import gq.example.dto.LoginSuccessDto;
import gq.example.dto.UpdateUserAndRoleDto;
import gq.example.entity.Users;
import gq.example.entity.UsersAndRole;
import gq.example.mapper.UsersAndRoleMapper;
import gq.example.result.PageResult;
import gq.example.result.Result;
import gq.example.service.IUsersService;
import gq.example.utils.JwtUtil;
import org.jose4j.lang.JoseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUsersService usersService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UsersAndRoleMapper usersAndRoleMapper;

    @PostMapping("/register")
    public Result addUser(@RequestBody Users users) {
        Users user = usersService.userExist(users.getUsername(), users.getEmail());
        if (user != null) {
            return Result.error("用户名或邮箱已存在");
        }

        Users newUser = new Users();
        newUser.setUsername(users.getUsername());
        newUser.setPassword(passwordEncoder.encode(users.getPassword()));
        newUser.setEmail(users.getEmail());
        newUser.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int j = usersService.addUser(newUser);
        if (j > 0) {
            return Result.success("注册成功");
        }

        return Result.error("服务异常");
    }

    @GetMapping("/findUsers")
    public Result findUsers(FindUserDto findUserDto) {
        PageHelper.startPage(findUserDto.getPage(), findUserDto.getSize());
        Users user = new Users(findUserDto.getUid(), findUserDto.getUsername());
        List<Users> users = usersService.findUsersByUsername(user);
        for (Users u : users) {
            u.setRid(usersAndRoleMapper.findRidByUid(u.getUid()));
        }
        PageInfo<Users> pageInfo = new PageInfo(users);
        return Result.success("查询成功", JSON.toJSON(new PageResult(pageInfo.getTotal(), pageInfo.getList())));
    }

    @DeleteMapping("/deleteUser")
    public Result deleteUser(@RequestParam("uid") Integer id) {
        int i = usersService.deleteUser(id);
        if (i > 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody Users user) {

        Users oldUser = usersService.findUserByUserId(user.getUid());

        if (oldUser == null) {
            return Result.error("用户不存在");
        }

        if (user.getPassword() != null) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }

        int i = usersService.updateUser(user);
        if (i > 0) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @PutMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody Users user) {
        System.out.println("---------------修改密码-------------");
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        int i = usersService.forgetPassword(user);
        if (i > 0) {
            return Result.success("密码修改成功");
        }
        return Result.error("密码修改失败");
    }

    @GetMapping("/findUser")
    public Result findUserByUsername(@RequestParam("username") String username) {
        Users user = usersService.findUserByUsername(username);
        if (user != null) {
            return Result.success("查询成功", user);
        }
        return Result.error("用户不存在");
    }

    @PutMapping("/updateUserAndRole")
    public Result UpdateUserAndRoleByUid(@RequestBody UpdateUserAndRoleDto updateUserAndRoleDto) {
        System.out.println(updateUserAndRoleDto.getUid());
        Users user = new Users(updateUserAndRoleDto.getUid(), updateUserAndRoleDto.getUsername(), updateUserAndRoleDto.getSectorid(), updateUserAndRoleDto.getIntroduce());
        int i = usersService.updateUser(user);
        List<UsersAndRole> usersAndRoles = new ArrayList<>();

        usersAndRoles.add(new UsersAndRole(updateUserAndRoleDto.getUid(), updateUserAndRoleDto.getRid()));
        int i1 = usersAndRoleMapper.updateRolesById(usersAndRoles);

        if(updateUserAndRoleDto.getUid() == null){
            return Result.error("用户id不能为空");
        }

        if (i > 0 || i1 > 0) {
            return Result.success("修改成功");
        }

        return Result.success("修改失败");
    }
}


